<?php
// +----------------------------------------------------------------------
// | Powered By ThinkPHP 5.0.12
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.bojuwang.net All rights reserved.
// +----------------------------------------------------------------------
// | Date：2018-1
// +----------------------------------------------------------------------
// | Author: ZhengLuwei <512711380@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Db;
class User extends AdminBasic
{
    /**
     * 加载用户信息列表
     */
    public function index()
    {
        $where = ['status'=>['<',2]];
        $keywords = input('param.keywords');
        $logmin = input('param.logmin');
        $logmax = input('param.logmax');
        $type = input('param.type');
        if (isset($type) && $type !='')
        {
            $where['type'] = $type;
            $this->assign('type',$type);
        }
        $keywords ? $where['user_name|mobile|user_nickname|true_name'] = ['like',"%{$keywords}%"] :'';
        $logmin ? $where['add_time'] = ['>',$logmin] :'';
        $logmax ? $where['add_time'] = ['<',$logmax] :'';

        if ($logmin && $logmax) {
            $where['add_time'] = ['between',[$logmin,$logmax]];
        }
        $userInfoList = Db::name('user')
            ->field('user_name,id,true_name,user_nickname,type,mobile,add_ip,add_time,status,balance,frozen')
            ->where($where)
            ->order('id desc')
            ->paginate(15,false,['query'=>request()->param()])
            ->each(function($item, $key){
                $item['previou'] = Db::name('user_distribution')
                    ->alias('b')
                    ->field('a.true_name,a.user_nickname')
                    ->join('user a','a.id=b.user_id','left')
                    ->where('b.next_user_id',$item['id'])
                    ->find();
                return $item;
            });
    /*    echo "<pre>";
        foreach ($userInfoList as $k=>$v) {
            $agent = get_user_agent($v['id']);
            $v['agent'] = $agent;
            $data[] = $v;
        }

        p($data);*/
        $balanceSum =  Db::name('user')->where($where)->sum('balance');
        $frozenSum =  Db::name('user')->where($where)->sum('frozen');
        $this->assign('balanceSum',$balanceSum);
        $this->assign('frozenSum',$frozenSum);
        $this->assign('userInfoList',$userInfoList);
        return view();
    }
    public function agent()
    {
        $userId = input('param.id');
        $data = get_user_agent($userId);
        $this->assign('list',$data);
        return $this->fetch();
    }
    /**
     * 添加会员信息
     */
    public function add()
    {
        return view();
    }
    /**
     * 编辑保存
     */
    public function editsave()
    {
        $userId=input('post.userid');
        $mobile=input('post.mobile');
        $email=input('post.email');
        $sex=input('post.sex');
        $nickname=input('post.nickname');
        $userSign=input('post.sign');
        $type=input('post.type');
        $userInfo=array();
        $userInfo['mobile']=$mobile;
        $userInfo['email']=$email;
        $userInfo['sex']=$sex;
        $userInfo['user_nickname']=$nickname;
        $userInfo['user_sign']=$userSign;
        $userInfo['type']=$type;
        $result=Db::name('user')->where('id',$userId)->update($userInfo);
        unset($userInfo);
        if($result==0)
        {
            json_msg(1,'编辑失败');
        } else {
            json_msg(0,'编辑成功');
        }
        
    }
    /**
     * 编辑会员信息
     * @param int 用户ID
     */
    public function edit()
    {
        $userId=input('get.id');
        $userInfo=Db::name('user')->where('id',$userId)->find();
        $this->assign('userInfo',$userInfo);
        return view();
    }
    /**
     * 展示会员信息
     */
    public function show()
    {
        return view();
    }
    /**
     * 加载修改密码页面
     * @param int 用户ID
     */
    public function changepassword()
    {
        $userId=input('get.id');
        $userInfo=Db::name('User')->where('id',$userId)->find();
        $this->assign('userInfo',$userInfo);
        return view();
    }
    /**
     * 修改密码
     * @param int $userid 用户ID
     * @param string $password 新的密码
     */
    public function changepasswordsave()
    {
        $userId=input('post.userid');
        $password=input('post.password');
        $userInfo=array();
        $userInfo['password']=md5($password);
        
        $result=Db::name('User')->where('id',$userId)->update($userInfo);
        unset($userInfo);
        if($result==0)
        {
            json_msg(1,'修改失败');
        }
        else{
            json_msg(0,'修改成功');
        }
    }
    /**
     * 渲染已删除会员列表
     */
    public function delete()
    {
        $userInfoList=Db::name('User')->where('status',2)->order('id desc')->paginate(15,false,['query'=>request()->param()]);
        $this->assign('userInfoList',$userInfoList);
        unset($userInfoList);
        return view();
    }
    /**
     * 彻底删除指定用户
     * @param int $userId 用户ID
     */
    public function lastdel()
    {
        $userId=input('post.userId');
        if(!isset($userId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('User')->where('id',$userId)->delete();
        if($result==0)
        {
            json_msg(1,'删除失败');
        }
        else
        {
            json_msg(0,'删除成功');
        }
    }
    
    /**
     * 删除指定用户
     * @param int $userId 用户ID
     */
    public function del()
    {
        $userId=input('post.userId');
        if(!isset($userId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('User')->where('id',$userId)->delete();
        // $result=Db::name('User')->where('id',$userId)->setField('status',2);
        if($result==0)
        {
            json_msg(1,'删除失败');
        }
        else
        {
            json_msg(0,'删除成功');
        }
    }


    public function selectUser()
    {
        $keywords = input('param.keywords');
        $userId = input('param.user_id');
        $where = ['id'=>['neq',$userId],'type'=>1];
        $where['user_name|mobile|user_nickname|true_name'] = ['like',"%{$keywords}%"];
        $user = Db::name('user')->field('id,user_nickname,true_name')->where($where)->select();
        json_msg_data(0,'获取成功',$user);
    }
    /**
     * 还原已经删除的用户
     * @param int $userId 用户ID
     */
    public function huanyuan()
    {
        $userId=input('post.userId');
        if(!isset($userId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('User')->where('id',$userId)->setField('status',0);
        if($result==0)
        {
            json_msg(1,'还原失败');
        }
        else
        {
            json_msg(0,'还原成功');
        }
    }
    /**
     * 禁止用户
     * @param int $userId 用户ID
     */
    public function stop()
    {
        $userId=input('post.userId');
        if(!isset($userId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('User')->where('id',$userId)->setField('status',1);
        if($result==0)
        {
            json_msg(1,'状态没有改变');
        }
        else
        {
            json_msg(0,'用户已停用');
        }
    }
    /**
     * 启用用户
     * @param int $userId 用户ID
     */
    public function start()
    {
        $userId=input('post.userId');
        if(!isset($userId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('User')->where('id',$userId)->setField('status',0);
        if($result==0)
        {
            json_msg(0,'状态没有改变');
        }
        else
        {
            json_msg(0,'用户已经启用');
        }
    }
    /**
     * 加载会员抽奖日志列表
     */
    public function prizeList()
    {
        $list=Db::name('prize_log')->order('status asc,prize desc ')->paginate(15,false,['query'=>request()->param()])->each(function($item, $key){
            $item['prizeshow']=config('prize_arr')[$item['prize']];
            $user=get_user_model($item['user_id']);
            $item['user_nickname']=$user['user_nickname'];
            $item['mobile']=$user['mobile'];
            return $item;
        });
        $this->assign('list',$list);
        return view();
    }
    public function prizeStop()
    {
        $id = input('param.prizeId');
        $res = Db::name('prize_log')->where('id',$id)->update(['status'=>0]);
        if ($res)
        {
            json_msg(0,'操作成功');
        } else {
            json_msg(1,'状态没有改变');
        }
    }
    public function prizeStart()
    {
        $id = input('param.prizeId');
        $res = Db::name('prize_log')->where('id',$id)->update(['status'=>1]);
        if ($res)
        {
            json_msg(0,'操作成功');
        } else {
            json_msg(1,'状态没有改变');
        }
    }
    /**
     * 删除某条抽奖日志
     * @param int $id 日志ID
     * @return json
     */
    public function deletePrizeLog()
    {
        $id=input('post.id');
        if(!isset($id))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('prize_log')->where('id',$id)->delete();
        if($result==0)
        {
            json_msg(1,'删除失败');
        }
        else
        {
            json_msg(0,'删除成功');
        }
        json_msg(1,'请求失败');
    }
    /**
     * 加载积分明细列表
     */
    public function pointList()
    {
        $list=Db::name('user_point')->order('id desc')->paginate(15,false,['query'=>request()->param()])->each(function($item, $key){
            $item['typeshow']=config('point_type_arr')[$item['type']];
            $user=get_user_model($item['user_id']);
            $item['user_nickname']=$user['user_nickname'];
            $item['true_name']=$user['true_name'];
            return $item;
        });
        $this->assign('list',$list);
        return view();
    }
    /**
     * 删除积分明细
     * @param int $id 明细ID
     */
    public function pointDelete()
    {
        $id=input('post.id');
        if(!isset($id))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('user_point')->where('id',$id)->delete();
        if($result==0)
        {
            json_msg(1,'明细删除失败');
        }
        else
        {
            json_msg(0,'明细删除成功');
        }
    }
    /**
     * 加载反馈信息列表
     */
    public function feedback()
    {
        $list=Db::name('feedback')->order('id desc')->paginate(15,false,['query'=>request()->param()])->each(function($item, $key){
            $user = get_user_model($item['user_id']);
            $item['user_nickname']=$user['user_nickname'];
            $item['true_name']=$user['true_name'];
            return $item;
        });
        $this->assign('list',$list);
        unset($list);
        return view();
    }
    /**
     * 删除指定反馈信息
     */
    public function feedback_del()
    {
        $id=input('post.id');
        if(!isset($id))
        {
           json_msg(1,'请求参数错误');
        }
        $result=Db::name('feedback')->where('id',$id)->delete();
        if($result==0)
        {
           json_msg(1,'删除失败');
        } else {
           json_msg(0,'删除成功');
        }
    }
    /**
     * 未处理反馈信息
     */
    public function feedback_stop()
    {
        $id=input('post.id');
        if(!isset($id))
        {
           json_msg(1,'请求参数错误');
        }
        $result=Db::name('feedback')->where('id',$id)->setField('status',1);
        if($result==0)
        {
           json_msg(1,'状态未更改');
        }
        else
        {
           json_msg(0,'已标记为未处理');
        }
    }
    /**
     * 处理反馈信息
     */
    public function feedback_start()
    {
        $id=input('post.id');
        if(!isset($id))
        {
           json_msg(1,'请求参数错误');
        }
        $result=Db::name('feedback')->where('id',$id)->setField('status',0);
        if($result==0)
        {
           json_msg(1,'状态未更改');
        }
        else
        {
           json_msg(0,'已标记为已处理');
        }
    }
    /*
     * 实名认证
     * */
    public function certify()
    {
        $keywords = input('param.keywords');
        $status = input('param.status');
        if ($status) {
        	$where['is_true_name'] = $status;
        	$this->assign('status',$status);
        }
        else {
        	$where['is_true_name'] = ['gt',0];
        }
        $keywords ? $where['user_name|mobile|user_nickname|true_name'] = ['like',"%".trim($keywords)."%"] :'';
        $data = Db::name('user')->where($where)->order('is_true_name desc,add_time desc')->paginate(15,false,['query'=>request()->param()]);
        $this->assign('list',$data);
        return $this->fetch();
    }
    /**
     * 禁止用户
     * @param int $userId 用户ID
     */
    public function certifyStop()
    {
        $userId=input('post.userId');
        if(!isset($userId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('User')->where('id',$userId)->setField('is_true_name',3);
        if($result==0)
        {
            json_msg(1,'状态没有改变');
        }
        else
        {
            json_msg(0,'审核失败');
        }
    }
    /**
     * 启用用户
     * @param int $userId 用户ID
     */
    public function certifyStart()
    {
        $userId=input('post.userId');
        if(!isset($userId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('User')->where('id',$userId)->setField('is_true_name',2);
        if($result==0)
        {
            json_msg(0,'状态没有改变');
        }
        else
        {
            json_msg(0,'审核成功');
        }
    }
    /*修改用户上级*/
    public function saveUserParent()
    {
        $userId = input('param.id');
        if (request()->isPost())
        {
            $parentid = input('param.parentid');
            $parent = Db::name('user_distribution')->where('next_user_id',$parentid)->find();
            $classList = $parent ? $parent['class_list']: ','.$parentid.',';
            $data = [
                'user_id'=>$parentid,
                'next_user_id'=>$userId,
                'class_list'=>$classList.$userId.',',
                'class_layer'=>$parent['class_layer'] + 1
            ];
            if (Db::name('user_distribution')->where('next_user_id',$userId)->find())
            {
                $res = Db::name('user_distribution')->where('next_user_id',$userId)->update($data);
            } else {
                $res = Db::name('user_distribution')->insert($data);
            }
            if ($res)
            {
                json_msg(0,'操作成功');
            } else {
                json_msg(1,'尚未更改或操作失败');
            }
        } else {
            $this->assign('userid',$userId);
            $parentid = Db::name('user_distribution')->where('next_user_id',$userId)->value('user_id');
            $this->assign('parentid',$parentid);
            $parent = Db::name('user')->field('id,user_nickname,true_name')->where('id',$parentid)->find();
            $this->assign('parent',$parent);

            return $this->fetch();
        }
    }
    /*会员流水*/
    public function balance()
    {
        $userId = input('param.id');
        $list = Db::name('user_balance')->alias('a')->field('a.*,b.user_nickname,b.true_name')->join('user b','a.user_id=b.id')->where('a.user_id',$userId)->order('a.add_time desc')->paginate(15,false,['query'=>request()->param()])->each(function($item, $key){
            $item['typeshow'] = config('balance_type_arr')[$item['type']];
            return $item;
        });;
        $this->assign('list',$list);
        return $this->fetch();
    }
}